package cn.zcf.product.service.impl;

import cn.zcf.basic.exception.BusinessException;
import cn.zcf.basic.service.IBaiduAiAuditService;
import cn.zcf.basic.service.impl.BaseServiceImpl;
import cn.zcf.product.domain.Product;
import cn.zcf.product.domain.ProductDetail;
import cn.zcf.product.mapper.ProductDetailMapper;
import cn.zcf.product.mapper.ProductMapper;
import cn.zcf.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Override
    public void add(Product product) {
        Boolean textBoo = baiduAiAuditService.textAudit(product.getName());
        Boolean imageBoo = true;
        if(textBoo && imageBoo){
            productMapper.add(product);
            ProductDetail detail = product.getProductDetail();
            detail.setProduct_id(product.getId());
            productDetailMapper.add(detail);
        }
        else {
            throw new BusinessException("数据不合法");
        }

    }

    @Override
    public void update(Product product) {
        Boolean textBoo = baiduAiAuditService.textAudit(product.getName());
        Boolean imageBoo = true;
        if(textBoo && imageBoo){
            productMapper.update(product);
            productDetailMapper.update(product.getProductDetail());
        }
        else {
            throw new BusinessException("操作失败");
        }

    }

    @Override
    public void onSale(List<Long> ids) {

        productMapper.onSale(ids,new Date());
    }

    @Override
    public void offSale(List<Long> ids) {
        productMapper.offSale(ids,new Date());
    }
}
